package com.by.basicgateway.enums;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.by.redis.enums.ByRedisUtilityEnum;
import com.by.redis.support.ByRedisSupport;
import com.by.toolkit.consts.RedisConst;
import com.by.toolkit.consts.WebGlobalConst;
import com.by.toolkit.enums.ErrorCodeEnum;
import com.by.toolkit.utils.ExceptionUtil;
import com.by.toolkit.utils.SplitterUtil;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Predicate;

import static com.by.toolkit.utils.ExceptionUtil.throwError;

@Getter
@Slf4j
@AllArgsConstructor
public enum RequestorClientTypeEnum {

    TOC_CLIENT(
            "tocClient",
            "authorization",
            WebGlobalConst.TOC_USER_ID,
            "c端客户端",
            paramMap -> true,
            tokenContentMap -> {

            }),

    TOB_CLIENT(
            "tobClient",
            "authorization",
            WebGlobalConst.TOB_USER_ID,
            "b端客户端",
            paramMap -> true,
            tokenContentMap -> {

            }),

    WX_CP_SIDEBAR_CLIENT(
            "wxCpSidebarClient",
            "authorization",
            WebGlobalConst.TOB_USER_ID,
            "企业微信侧边栏端客户端",
            paramMap -> true,
            tokenContentMap -> {
                String uuidStr = tokenContentMap.getString("uuid");
                if (StringUtils.isBlank(uuidStr)) {
                    log.info("[RequestorClientTypeEnum] 侧边栏token生成参数异常,无法进行数据数据验证 ");
                    throwError(ErrorCodeEnum.EXPIRE_TYPE);
                }
                String sysUserId = tokenContentMap.getString(WebGlobalConst.TOB_USER_ID);
                if (StringUtils.isBlank(sysUserId)) {
                    log.info("[RequestorClientTypeEnum] 侧边栏token生成参数异常,无法进行数据数据验证 ");
                    throwError(ErrorCodeEnum.EXPIRE_TYPE);
                }
                ByRedisSupport redisSupport = SpringUtil.getBean(ByRedisSupport.class);
                StringRedisTemplate stringRedisTemplate = redisSupport.getStringRedisTemplate(ByRedisUtilityEnum.COMMON);
                String effectUuid = stringRedisTemplate.opsForValue()
                        .get(SplitterUtil.EN_COLON_JOINER.join(RedisConst.ADMIN_TOKEN_FORCE_LOSE_EFFECT_KEY,sysUserId));
                if (!StringUtils.equals(uuidStr,effectUuid)) {
                    log.info("[RequestorClientTypeEnum] 侧边栏token已经失效,无法进行数据数据验证 ");
                    ExceptionUtil.throwError(ErrorCodeEnum.EXPIRE_TYPE);
                }
            }),
    ;

    private String clientHeaderValue;

    private String tokenHeaderName;

    private String userIdName;

    private String desc;

    private Predicate<Map<String, List<String>>> validParamPredicate;

    private Consumer<JSONObject> tokenParamPredicate;

    public static final AtomicReference<ByRedisSupport> REDIS_SUPPORT = new AtomicReference<>();

    public static final String CLIENT_HEADER_NAME = "clientHeaderName";

    private static final Map<String, RequestorClientTypeEnum> MAP = Maps.newHashMap();

    static {

        for (RequestorClientTypeEnum value : RequestorClientTypeEnum.values()) {
            MAP.put(value.getClientHeaderValue(), value);
        }

    }

    public static RequestorClientTypeEnum resolveRequestorClientTypeEnum(String clientHeaderValue) {
        return MAP.get(clientHeaderValue);
    }

}
